package com.ymatou.i.accounting.api.service;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ymatou.i.accounting.api.parameter.AccountBean;
import com.ymt.base.YmatouBaseCall;
import com.ymttest.utils.EnvSetup;

/**
 * 根据用户编号及账户类型获取账户信息接口Call FreeMaker Template自动生成代码
 */
public class GetAccountByUserIdCall extends YmatouBaseCall {
	private static String opurl = "api/Account/GetAccountByUserId";

	public GetAccountByUserIdCall() {
		super(EnvSetup.getSystemUrl("api_accounting"), opurl, "POST", "JSON");
	}

	@Override
	public void addDefHeader() {
		client.addHeader("Accept",
				"text/xml,text/javascript,text/html,application/json");
		client.addHeader("Content-Type", "application/json; charset=UTF-8");
	}

	// 获取 accountid
	public String getAccountId() throws JSONException {
		String cid = null;
		cid = new JSONObject(getReturnData()).getJSONObject("Data").getString(
				"AccountId");
		return cid;
	}

	// get data
	public String getData() throws JSONException {
		String data = null;
		data = new JSONObject(getReturnData()).getString("Data");
		return data;
	}

	// get the return account info list
	public AccountBean getReturnAccount() throws JSONException {
		AccountBean account = null;
		String data = getData();
		if (data != null) {
			Gson gson = new Gson();
			account = gson.fromJson(data, AccountBean.class);
		}
		return account;
	}

	// get the return account info list
	public ArrayList<AccountBean> getReturnAccountList() throws JSONException {
		ArrayList<AccountBean> list = new ArrayList<AccountBean>();
		String data = getData();
		if (data != null) {
			Gson gson = new Gson();
			list = gson.fromJson(data, new TypeToken<ArrayList<AccountBean>>() {
			}.getType());
		}
		return list;
	}
}